package com.videoaggre.zcl.helper.mohsi

import com.squareup.moshi.*
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.converter.moshi.MoshiConverterFactory
import java.io.IOException
import java.lang.reflect.Type

val MOSHI = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .add(MoshiArrayListJsonAdapter.FACTORY)
    .build()
fun getMoshiConverterFactory(): MoshiConverterFactory = MoshiConverterFactory.create(MOSHI)
//class EvaluateLevelAdapter {
//    @FromJson
//    @Throws(IOException::class)
//    fun fromJson(value: Int): EvaluateLevelEnum {
//        for (item in EvaluateLevelEnum.values()) {
//            if (item.level == value) return item
//        }
//        return EvaluateLevelEnum.ALL
//    }
//
//    @ToJson
//    fun toJson(value: EvaluateLevelEnum?): Int {
//        return value?.level ?: EvaluateLevelEnum.ALL.level
//    }
//}

//class LogisticsStateAdapter {
//    @FromJson
//    @Throws(IOException::class)
//    fun fromJson(value: Int): LogisticsStateEnum {
//        for (item in LogisticsStateEnum.values()) {
//            if (item.state == value) return item
//        }
//        return LogisticsStateEnum.STATE1
//    }
//
//    @ToJson
//    fun toJson(value: LogisticsStateEnum?): Int {
//        return value?.state ?: LogisticsStateEnum.STATE1.state
//    }
//}
//
//class GoodsOrderStateAdapter {
//    @FromJson
//    @Throws(IOException::class)
//    fun fromJson(value: Int): GoodsOrderStateEnum {
//        for (item in GoodsOrderStateEnum.values()) {
//            if (item.state == value) return item
//        }
//        return GoodsOrderStateEnum.ALL
//    }
//
//    @ToJson
//    fun toJson(value: GoodsOrderStateEnum?): Int {
//        return value?.state ?: GoodsOrderStateEnum.ALL.state
//    }
//}

abstract class MoshiArrayListJsonAdapter<C : MutableCollection<T>?, T> private constructor(
    private val elementAdapter: JsonAdapter<T>
) : JsonAdapter<C>() {
    abstract fun newCollection(): C

    @Throws(IOException::class)
    override fun fromJson(reader: JsonReader): C {
        val result = newCollection()
        reader.beginArray()
        while (reader.hasNext()) {
            result?.add(elementAdapter.fromJson(reader)!!)
        }
        reader.endArray()
        return result
    }

    @Throws(IOException::class)
    override fun toJson(writer: JsonWriter, value: C?) {
        writer.beginArray()
        for (element in value!!) {
            elementAdapter.toJson(writer, element)
        }
        writer.endArray()
    }

    override fun toString(): String {
        return "$elementAdapter.collection()"
    }

    companion object {
        val FACTORY = Factory { type, annotations, moshi ->
            val rawType = Types.getRawType(type)
            if (annotations.isNotEmpty()) return@Factory null
            if (rawType == ArrayList::class.java) {
                return@Factory newArrayListAdapter<Any>(type, moshi).nullSafe()
            }
            null
        }

        private fun <T> newArrayListAdapter(
            type: Type, moshi: Moshi
        ): JsonAdapter<MutableCollection<T>> {
            val elementType = Types.collectionElementType(type, MutableCollection::class.java)
            val elementAdapter: JsonAdapter<T> = moshi.adapter(elementType)
            return object : MoshiArrayListJsonAdapter<MutableCollection<T>, T>(elementAdapter) {
                override fun newCollection(): MutableCollection<T> {
                    return ArrayList()
                }
            }
        }
    }
}
